package com.resolution.dietaryassessment.view.tcm;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.resolution.dietaryassessment.BaseActivity;
import com.resolution.dietaryassessment.R;
import com.resolution.dietaryassessment.listener.MyListener;
import com.resolution.dietaryassessment.utils.FragAndActivityCommunication;
import com.resolution.dietaryassessment.adapter.MyFragmentPagerAdapter;
import com.resolution.dietaryassessment.view.tcm.fragment.ConstitutionIntroductionFrag;
import com.resolution.dietaryassessment.view.tcm.fragment.TcmResultFragment;
import com.resolution.dietaryassessment.view.tcm.fragment.TcmSubjectFragment;
import com.resolution.dietaryassessment.view.tcm.fragment.TCMIntroductionFragment;

import java.util.ArrayList;
import java.util.List;

/**
 *  @author By Resolusion
 *  @Title: TCMConstitutionActivity
 *  中医体质检测
 *  @Date: 2020/1/8
 *  @Time: 13:36
 *  @email res01ution@foxmail.com
 */
public class TCMConstitutionActivity extends BaseActivity implements FragAndActivityCommunication {

    private int fragmentIndex = 0;
    List<Fragment> mFragments = new ArrayList<>();
    ViewPager2 mViewPager2;
    Button mBack;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tcmconstitution);
        initView();
    }

    public void initView(){
        mBack = findViewById(R.id.back_btn);
        Button mPagodaBtn = findViewById(R.id.dietary_pagoda);
        Button mMydietary = findViewById(R.id.my_dietary);
        Button mLeave = findViewById(R.id.leave_btn);

        mPagodaBtn.setOnClickListener(new MyListener(this));
        mMydietary.setOnClickListener(new MyListener(this));
        mLeave.setOnClickListener(new MyListener(this));
        mBack.setOnClickListener(v -> {
            mViewPager2.setCurrentItem(fragmentIndex);
            mBack.setVisibility(View.GONE);
        });

        mViewPager2 = findViewById(R.id.tab_recycler);

        mFragments.add(new TCMIntroductionFragment());
        mFragments.add(new TcmSubjectFragment());
        mFragments.add(new TcmResultFragment());
        mFragments.add(new ConstitutionIntroductionFrag());

        MyFragmentPagerAdapter mAdapter = new MyFragmentPagerAdapter(this, mFragments);
        mViewPager2.setUserInputEnabled(false);
        mViewPager2.setAdapter(mAdapter);
    }

    @Override
    public void onNextFragment(boolean isNormal) {
        if (isNormal) {
            fragmentIndex = (fragmentIndex + 1) % mFragments.size();
            mViewPager2.setCurrentItem(fragmentIndex);
            mBack.setVisibility(View.GONE);
        } else {
            mBack.setVisibility(View.VISIBLE);
            mViewPager2.setCurrentItem(mFragments.size()-1);
        }
    }
}
